﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using WFNetLib;
using WFNetLib.Log;

namespace 模拟呼叫
{
    public class CallThread
    {
        public SerialPort Com;
        public Thread CallThreadIns = null;
        public static event intstrEventHandler AddLogEvent;//事件声明

        public string PortName;
        public int ComIndex;
        public bool bStop = false;
        public bool bClose = false;
        float t;
        public string[] UserName;
        public string[] Password;
        public bool bOther;
        void OnAddLog(string strLog, bool bSave)
        {
            if (AddLogEvent != null)
            {
                AddLogEvent(this, new intstrEventArgs(ComIndex, DateTime.Now.ToString("HH:mm:ss") + ":   " + strLog));
            }
            if (bSave)
            {
                DateTime dt = DateTime.Now;
                TextLog.AddTextLog(strLog, System.Windows.Forms.Application.StartupPath + "\\TextLog\\" + String.Format("{0:D4}", dt.Year) + "-" + String.Format("{0:D2}", dt.Month) + "-" + String.Format("{0:D2}", dt.Day) + "通道"+Com.PortName+"运行信息.txt", false);
            }
        }
        void OnAddLog(string strLog)
        {
            OnAddLog(strLog, true);
        }
        public CallThread(string _Com, int index,bool _bOther)
        {
            Com = new SerialPort(_Com);
            ComIndex = index;
            Com.RtsEnable = true;
            Com.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(COM_DataReceived);
            
            UserName = new string[30];
            Password = new string[30];
            bOther = _bOther;
            if(bOther)
                t = 29.9f;
            else
                t = 11.0f;
            //11度到19度温度点
            UserName[0] = "4000011254";//11
            Password[0] = "2Wv4Bf";
            UserName[1] = "4000011274";//12
            Password[1] = "AT2DQi";
            UserName[2] = "4000011324";//13
            Password[2] = "DB4PPG";
            UserName[3] = "4000011364";//14
            Password[3] = "kqQW7f";
            UserName[4] = "4000011374";//15
            Password[4] = "FdcdQ4";
            UserName[5] = "4000011424";//16
            Password[5] = "ATLHGh";
            UserName[6] = "4000011434";//17
            Password[6] = "IkLNUQ";
            UserName[7] = "4000011474";//18
            Password[7] = "fsCEVg";
            UserName[8] = "4000011524";//19
            Password[8] = "DfnYUT";

            //20度到29度温度点
            UserName[9] = "4000011534";//20
            Password[9] = "4RFDZK";
            UserName[10] = "4000011564";//21
            Password[10] = "TDlwFh";
            UserName[11] = "4000011574";//22
            Password[11] = "CaEpnX";
            UserName[12] = "4000011594";//23
            Password[12] = "9moYWy";
            UserName[13] = "4000011614";//24
            Password[13] = "j2qGb6";
            UserName[14] = "4000011624";//25
            Password[14] = "imyXPX";
            UserName[15] = "4000011634";//26
            Password[15] = "ZzeJDu";
            UserName[16] = "4000011704";//27
            Password[16] = "IRJlvR";
            UserName[17] = "4000011714";//28
            Password[17] = "9mP8b5";
            UserName[18] = "4000011724";//29
            Password[18] = "InvpDT";

            //小数温度点
            UserName[19] = "4000413804";//0
            Password[19] = "4QjGjY";
            UserName[20] = "4000413814";//1
            Password[20] = "gcxtjc";
            UserName[21] = "4000413824";//2
            Password[21] = "FH48O3";
            UserName[22] = "4000413834";//3
            Password[22] = "cfp3pw";
            UserName[23] = "4000414154";//4
            Password[23] = "ZRwJdE";
            UserName[24] = "4000413854";//5
            Password[24] = "grN9bu";
            UserName[25] = "4000413864";//6
            Password[25] = "GvXOlH";
            UserName[26] = "4000413874";//7
            Password[26] = "YCoweF";
            UserName[27] = "4000414124";//8
            Password[27] = "HBdDuS";
            UserName[28] = "4000414134";//9
            Password[28] = "oZEQbT";
            //申告号码
            UserName[29] = "4000011074";//
            Password[29] = "VTNZR4";
        }
        void COM_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string atres = Com.ReadExisting();
            OnAddLog(atres);
        } 
        public void CallThreadStart()
        {
            CallThreadIns = new Thread(new ThreadStart(CallProc));
            CallThreadIns.Name = "呼叫" + Com.PortName;
            PortName = Com.PortName;
            CallThreadIns.Start();
            OnAddLog("呼叫开始");
        }
        private void CallProc()
        {
            //try
            {
                try
                {
                    Com.Open();
                    //Com.RtsEnable = true;
                    OnAddLog("开始初始化\r\n");
                    while (!Com.CtsHolding) ;
                    Com.Write("ATE0" + "\r");
                    OnAddLog("发送ATE0\r\n");
                    Thread.Sleep(1000);
                }
                catch
                {
                    OnAddLog(Com.PortName+"串口无法打开,请终止软件");
                    return;
                }
                while (!bStop)
                {
                    OnAddLog("产生温度" + t.ToString("f1")+"\r\n",true);
                    //while (!Com.CtsHolding) ;
                    int z = (int)t;
                    string str = UserName[z - 11];
                    Com.Write("ATD" + str + ";" + "\r");
                    if (bOther)
                    {
                        OnAddLog("呼叫ATD" + str + ";且延时20s后挂机\r\n", true);
                        Thread.Sleep(20000);
                    }
                    else
                    {
                        OnAddLog("呼叫ATD" + str + ";且延时15s后挂机\r\n", true);
                        Thread.Sleep(15000);
                    }
                    //while (!Com.CtsHolding) ;
                    Com.Write("ATH" + "\r");
                    OnAddLog("挂机;且延时3s\r\n", true);
                    Thread.Sleep(3000);
                    int x = (int)((t * 10) % 10);
                    str = UserName[x + 19];
                    Com.Write("ATD" + str + ";" + "\r");
                    if (bOther)
                    {
                        OnAddLog("呼叫ATD" + str + ";且延时20s后挂机\r\n", true);
                        Thread.Sleep(20000);
                    }
                    else
                    {
                        OnAddLog("呼叫ATD" + str + ";且延时15s后挂机\r\n", true);
                        Thread.Sleep(15000);
                    }
                    //while (!Com.CtsHolding) ;
                    Com.Write("ATH" + "\r");
                    OnAddLog("挂机;且延时3s\r\n", true);
                    Thread.Sleep(3000);
                    OnAddLog("下次呼叫间隔延时60s\r\n", true);
                    Thread.Sleep(60000);
                    if(bOther)
                    {
                        t = t - 0.1f;
                        if (t <11.0f)
                            t = 29.9f;
                    }
                    else
                    {
                        t = t + 0.1f;
                        if (t > 29.9f)
                            t = 11.0f;
                    }
                    OnAddLog("---------------------------------------", true);
                }
                Com.DataReceived -= COM_DataReceived;
                Com.Close();
                bClose = true;
            }
        }
    }
    public enum FinishType
    {
        Finish = 0,
        DoAgain = 1,
        Unkown = 2
    }
}
